package com.ophiux.cs.application.user.impl.recore;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.ResultEnum;
import com.ophiux.cs.application.core.Throws.ParamException;
import com.ophiux.cs.application.user.dto.addUserRecoreRequest;
import com.ophiux.cs.application.user.dto.addUserRecoreResponse;
import com.ophiux.cs.domain.user.model.entity.UserLoginRecore;
import com.ophiux.cs.domain.user.repositories.IUserRecoreRepository;
import com.ophiux.utils.common.DateUtil;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class AddUserRecoreService {

	@Autowired
	IUserRecoreRepository iUserRecoreRepository;
	
	public Result<addUserRecoreResponse> addUserRecore(addUserRecoreRequest dto) {
//		Result<addUserRecoreResponse> result = new Result<addUserRecoreResponse>();
		addUserRecoreResponse response = new addUserRecoreResponse();
		try {
			
			try {
				/**
				 * 验证参数非空
				 */
				dto.paramVer();
			} catch (ParamException e) {
				return Result.fail(ResultEnum.requestParamFail.getCode(), e.getExceptionMessage());
			}
			
			String nowDate = DateUtil.getStrDate(new Date(), DateUtil.DateFormat.YYYY_MM_DD_HH_mm_ss);
			
			/** 
			 * 组建用户登录记录 实体信息
			 */
			UserLoginRecore recore = new UserLoginRecore();
			recore.setUSER_ID(dto.getUSER_ID());
			recore.setLOGIN_TOKEN(dto.getLOGIN_TOKEN());
			recore.setLOGIN_TIME(nowDate);
			
			boolean loadResult = iUserRecoreRepository.loadUserRecore(recore);
			
//			if(loadResult) {//登录记录信息 记录成功
//				
//			}else {//登录记录信息 记录成功
//				
//			}
			response.setRESULT(loadResult);
			
			return Result.success(response);
		} catch (Exception e) {
			log.info("addUserRecore接口异常",e);
			e.printStackTrace();
			return Result.fail(ResultEnum.requestException);
		}
	}
	
}
